﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

/// <summary>
/// Summary description for Setting
/// </summary>
public static class Setting
{

    private static string _CronExpression;
    public static string CronExpression
    {
        get { return _CronExpression; }
        set { _CronExpression = value; }
    }

    private static string _PathStatistic = "~/statistic_hit.xml";
    public static string PathStatistic
    {
        get { return _PathStatistic; }
        set { _PathStatistic = value; }
    }

    private static string _PathBanner = "~/upload/xml/banner.xml";
    public static string PathBanner
    {
        get { return _PathBanner; }
        set { _PathBanner = value; }
    }

    private static string _PathContact = "~/upload/xml/contact.xml";
    public static string PathContact
    {
        get { return _PathContact; }
        set { _PathContact = value; }
    }

    private static string _PathFooter = "~/upload/xml/copyrights.xml";
    public static string PathFooter
    {
        get { return _PathFooter; }
        set { _PathFooter = value; }
    }

    private static string _PathAdmin = "administrator/uploaded/";
    public static string PathAdmin
    {
        get { return _PathAdmin; }
        set { _PathAdmin = value; }
    }

    private static string _PathTemplateMail = "~/upload/xml/templatemail.xml";
    public static string PathTemplateMail
    {
        get { return _PathTemplateMail; }
        set { _PathTemplateMail = value; }
    }

    private static string _PathTemplateMailActive = "~/upload/xml/templatemailactive.xml";
    public static string PathTemplateMailActive
    {
        get { return _PathTemplateMailActive; }
        set { _PathTemplateMailActive = value; }
    }

    private static string _Path = "upload/";
    public static string Path
    {
        get { return _Path; }
        set { _Path = value; }
    }

    private static int online;

    public static int Online
    {
        get { return online; }
        set { online = value; }
    }
    private static int visited;

    public static int Visited
    {
        get { return visited; }
        set { visited = value; }
    }
    private static int today;

    public static int Today
    {
        get { return today; }
        set { today = value; }
    }

    private static int thisWeek;

    public static int ThisWeek
    {
        get { return thisWeek; }
        set { thisWeek = value; }
    }

    private static int thisMonth;

    public static int ThisMonth
    {
        get { return thisMonth; }
        set { thisMonth = value; }
    }

    private static string day;

    public static string Day
    {
        get { return day; }
        set { day = value; }
    }

    private static string crDay;

    public static string CrDay
    {
        get { return crDay; }
        set { crDay = value; }
    }

    private static string week;

    public static string Week
    {
        get { return week; }
        set { week = value; }
    }

    private static string crWeek;

    public static string CrWeek
    {
        get { return crWeek; }
        set { crWeek = value; }
    }

    private static string month;

    public static string Month
    {
        get { return month; }
        set { month = value; }
    }

    private static string crMonth;

    public static string CrMonth
    {
        get { return crMonth; }
        set { crMonth = value; }
    }

    public static void SetMetaTags(string title, Page page)
    {
        page.Title = title;
    }

    public static void SetMetaTags(string title, Page page, string keywords, string description)
    {
        page.Title = title;
        HtmlMeta meta = new HtmlMeta();
        meta.Name = "keywords";
        meta.Content = keywords;
        page.Header.Controls.Add(meta);
        meta.Name = "description";
        meta.Content = description;
        page.Header.Controls.Add(meta);
    }
    public static string setSrc(string file, string alt, string clas, string url, string height, string width)
    {
        string extension = System.IO.Path.GetExtension(file);
        if (extension == ".swf")
            file = "<object height='" + height + "' border='0' width='" + width + "' codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"><param value=\"" + url + file + "\" name=\"movie\"><param value=\"always\" name=\"AllowScriptAccess\"><param value=\"High\" name=\"quality\"><param value=\"transparent\" name=\"wmode\"><param value=\"#\" name=\"FlashVars\"><embed  height=' 100' border='0' width='300' allowscriptaccess=\"always\" wmode=\"transparent\" loop=\"true\" play=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" flashvars=\"#\" src=\"" + url + file + "\"></object>";
        else file = "<img src='" + url + file + "' alt='" + alt + "' onerror='ImgError(this);' class='" + clas + "' height='" + height + "' />";
        return file;
    }

    public static string getImg(string src)
    {
        if (!src.Trim().StartsWith("http://"))
            src = "/" + Path + src;
        return src;
    }

    public static string getRequest(HttpRequest request, string req, string nullval)
    {
        string val;
        if (!string.IsNullOrEmpty(request[req]))
            val = request[req].ToString();
        else
            val = nullval;
        return val;
    }

    public static int getRequest(HttpRequest request, string req, int nullval)
    {
        int val;
        if (!string.IsNullOrEmpty(request[req]))
            val = Convert.ToInt32(request[req]);
        else
            val = nullval;
        return val;
    }

    public static void bindDDL(DropDownList ddl, DataTable objDT, string text, string value)
    {
        if (objDT != null && objDT.Rows.Count > 0)
        {
            ddl.DataSource = objDT;
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
        }
        else
            if (ddl.Items.Count == 0)
                ddl.Items.Add(new ListItem("----------", "-1"));
    }
    public static void bindDDL(DropDownList ddl, DataTable objDT, string text, string value, string nullText, string nullValue)
    {
        if (objDT != null && objDT.Rows.Count > 0)
        {
            ddl.DataSource = objDT;
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
        }
        else
            if (ddl.Items.Count == 0)
                ddl.Items.Add(new ListItem(nullText, nullValue));
    }
    //--------------------BindReapeater
    public static void bindRepeater(Repeater rpt, DataTable objDT)
    {
        try
        {
            if (objDT != null && objDT.Rows.Count > 0)
            {
                rpt.DataSource = objDT;
                rpt.DataBind();
            }
            else
            {
                rpt.DataSource = null;
                rpt.DataBind();
            }
        }
        catch { }
    }

    public static void statistic(HttpContext context)
    {
        if (Convert.ToBoolean(context.Session["Hit"]))
        {
            context.Application.Lock();
            Day = DateTime.Now.Day.ToString();
            System.Globalization.CultureInfo cult_info = System.Globalization.CultureInfo.CreateSpecificCulture("no");
            System.Globalization.Calendar cal = cult_info.Calendar;
            Week = cal.GetWeekOfYear(DateTime.Now, cult_info.DateTimeFormat.CalendarWeekRule, cult_info.DateTimeFormat.FirstDayOfWeek).ToString();
            Month = DateTime.Now.Month.ToString();
            if (string.IsNullOrEmpty(CrDay))
            {
                CrDay = day;
            }
            if (string.IsNullOrEmpty(CrWeek))
            {
                CrWeek = Week;
            }
            if (string.IsNullOrEmpty(CrMonth))
            {
                CrMonth = Month;
            }
            Online = Online + 1;
            System.Xml.XmlDocument document = new System.Xml.XmlDocument();
            document.Load(context.Server.MapPath(PathStatistic));
            System.Xml.XmlNode node = document["HanoiRoad"];
            if (CrDay == Day)
                Today = Convert.ToInt32(node["Today"].InnerText) + 1;
            else
            {
                CrDay = Day;
                Today = 1;
            }
            if (CrWeek == Week)
                ThisWeek = Convert.ToInt32(node["Week"].InnerText) + 1;
            else
            {
                CrWeek = Week;
                ThisWeek = 1;
            }
            if (CrMonth == Month)
                ThisMonth = Convert.ToInt32(node["Month"].InnerText) + 1;
            else
            {
                CrMonth = Month;
                ThisMonth = 1;
            }
            Visited = Convert.ToInt32(node["Total"].InnerText) + 1;
            context.Session["Hit"] = false;
            context.Application.UnLock();
            System.Xml.XmlTextWriter objX = new System.Xml.XmlTextWriter(context.Server.MapPath(PathStatistic), System.Text.Encoding.UTF8);
            objX.WriteStartDocument();
            objX.WriteStartElement("HanoiRoad");
            objX.WriteElementString("Day", Day);
            objX.WriteElementString("Today", Today.ToString());
            objX.WriteElementString("Week", ThisWeek.ToString());
            objX.WriteElementString("Month", ThisMonth.ToString());
            objX.WriteElementString("Total", Visited.ToString());
            objX.WriteEndElement();
            objX.WriteEndDocument();
            objX.Flush();
            objX.Close();
        }
    }


    //Datetime

    public static int GetDaysInMonth(int month, int year)
    {
        if (month < 1 || month > 12)
        {
            throw new System.ArgumentOutOfRangeException("month", month, "month mustbe between 1 and 12");
        }
        if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month ||
        10 == month || 12 == month)
        {
            return 31;
        }
        else if (2 == month)
        {
            // Check for leap year
            if (0 == (year % 4))
            {
                // If date is divisible by 400, it's a leap year.
                // Otherwise, if it's divisible by 100 it's not.
                if (0 == (year % 400))
                {
                    return 29;
                }
                else if (0 == (year % 100))
                {
                    return 28;
                }

                // Divisible by 4 but not by 100 or 400
                // so it leaps
                return 29;
            }
            // Not a leap year
            return 28;
        }
        return 30;
    }
    public static string Upload(string folder, FileUpload ful, HttpContext context)
    {
        string fileName = string.Empty;
        if (ful.HasFile)
        {
            try
            {
                string FolderYear;
                string FolderMonth;
                string FolderDay;
                DateTime now = DateTime.Now;
                FolderYear = now.Year.ToString();
                FolderMonth = now.Month.ToString();
                FolderDay = now.Day.ToString();
                if (!Directory.Exists(context.Server.MapPath("~/upload/" + folder)))
                    Directory.CreateDirectory(context.Server.MapPath("~/upload/" + folder));
                if (!Directory.Exists(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear)))
                    Directory.CreateDirectory(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear));
                if (!Directory.Exists(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth)))
                    Directory.CreateDirectory(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth));
                if (!Directory.Exists(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay)))
                    Directory.CreateDirectory(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay));
                fileName = ful.FileName;
                int count = 0;
                while (File.Exists(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay + "/" + fileName)))
                {
                    fileName = count + "_" + ful.FileName;
                    count++;
                }
                ful.SaveAs(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay + "/" + fileName));
                return folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay + "/" + fileName;
            }
            catch { return fileName; }
        }
        else
            return fileName;
    }
    public static string Upload(string folder, HttpPostedFile hpf, HttpContext context)
    {
        string fileName = string.Empty;
        if (hpf.ContentLength > 0)
        {
            try
            {
                string FolderYear;
                string FolderMonth;
                string FolderDay;
                DateTime now = DateTime.Now;
                FolderYear = now.Year.ToString();
                FolderMonth = now.Month.ToString();
                FolderDay = now.Day.ToString();
                if (!Directory.Exists(context.Server.MapPath("~/upload/" + folder)))
                    Directory.CreateDirectory(context.Server.MapPath("~/upload/" + folder));
                if (!Directory.Exists(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear)))
                    Directory.CreateDirectory(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear));
                if (!Directory.Exists(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth)))
                    Directory.CreateDirectory(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth));
                if (!Directory.Exists(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay)))
                    Directory.CreateDirectory(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay));
                fileName = System.IO.Path.GetFileName(hpf.FileName);
                int count = 0;
                while (File.Exists(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay + "/" + fileName)))
                {
                    fileName = count + "_" + hpf.FileName;
                    count++;
                }
                hpf.SaveAs(context.Server.MapPath("~/upload/" + folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay + "/" + fileName));
                return folder + "/" + FolderYear + "/" + FolderMonth + "/" + FolderDay + "/" + fileName;
            }
            catch { return fileName; }
        }
        else
            return fileName;
    }
}